// packageC/pages/package/agv_manage/lineManage/addLine/addLine.js
const mybehavior = require("../../../../../../behaviors/requests")
import WxValidate from "../../../../../../Wx_Validate/WxValidate"
Page({

  /**
   * 页面的初始数据
   */
  behaviors: [mybehavior],
  data: {
    baseUrl: "https://logisticsengineering.gree.com/agv_dispatch_system",
    function_card_info_show: [
      {
        card: "",
        instruct: ""
      }
    ],
    instruct_map: [
      { "instruct_code": 0, "instruct": "无动作" },
      { "instruct_code": 1, "instruct": "停车" },
      { "instruct_code": 2, "instruct": "左转" },
      { "instruct_code": 3, "instruct": "右转" },
      { 'instruct_code': 4, 'instruct': "高速" },
      { "instruct_code": 5, "instruct": "中速" },
      { "instruct_code": 6, "instruct": "低速" },
      { "instruct_code": 7, "instruct": "临停" },
    ],
    post_table_arr: [

    ],
    department: "",
    magnetic_route_name: "",
    start_card: "",
    options1: [],
    activeNames1: [],
    activeNames2: [],
    activeNames3: [],
    activeNames4: [],
    selected_storage: [],
    options_storage_name: [],
    agv_name_options: [],
    current_options_post: [],
    post_station_id_list: [],
    storage_rack_id_list: [],
    special_agv_list: [],
    agv_list: [],
    submit_disabled: false,
    function_card_info: []
  },
  // 途径货架绑定选中值
  getSelectBox(e) {
    let arr = JSON.parse(JSON.stringify(this.data.options_storage))
    let storage_rack_id_list = []
    let key = e.currentTarget.id
    for (let i = 0; i < arr.length; i++) {
      for (let j = 0; j < e.detail.length; j++) {
        if (e.detail[j] == arr[i].storage_rack_name) {
          storage_rack_id_list.push(arr[i].key)
        }
      }
    }
    this.setData({
      [key]: e.detail,
      storage_rack_id_list: storage_rack_id_list
    })
  },
  // 分配车辆选择值
  getSelectBoxAgv(e) {
    let arr = JSON.parse(JSON.stringify(this.data.agv_options))
    let special_agv_list = []
    let key = e.currentTarget.id
    for (let i = 0; i < arr.length; i++) {
      for (let j = 0; j < e.detail.length; j++) {
        if (e.detail[j] == arr[i].name) {
          special_agv_list.push(arr[i].key)
        }
      }
    }
    this.setData({
      [key]: e.detail,
      special_agv_list: special_agv_list
    })
  },
  onActiveChange(event) {
    let key = 'activeNames' + event.currentTarget.dataset.key
    this.setData({
      [key]: event.detail,
    });
  },
  // 新增岗位按钮
  addFuncBtnPost() {
    let obj = {
      line: "",
      options: ""
    }
    let lists = this.data.post_table_arr
    lists.push(obj)
    this.setData({
      post_table_arr: lists,
      current_options_post: []
    })

  },
  // 删除岗位按钮
  delFuncBtnPost(e) {
    console.log(e.target.dataset.row);
    let index = e.target.dataset.row
    let lists = this.data.post_table_arr
    lists.splice(index, 1)
    this.setData({
      post_table_arr: lists,
      current_options_post: []

    })

  },
  // 途径岗位站点选择
  getPickerVlauePost(e) {
    let index = e.target.dataset.index
    let key = e.target.id
    let arr = JSON.parse(JSON.stringify(this.data.post_table_arr))
    let post_arr = JSON.parse(JSON.stringify(this.data.post_options))
    let post_station_id_list = JSON.parse(JSON.stringify(this.data.post_station_id_list))
    arr[index][key] = e.detail
    let picket_post_arr = []
    if (key == 'line') {
      for (let i = 0; i < post_arr.length; i++) {
        if (post_arr[i].line == e.detail) {
          picket_post_arr.push(post_arr[i].material_name)
        }

      }
      arr[index].options = ""
      this.setData({
        current_options_post: picket_post_arr,
        post_table_arr: arr
      })
    } else {
      for (let i = 0; i < post_arr.length; i++) {
        if (post_arr[i].material_name == e.detail) {
          post_station_id_list.push(post_arr[i].key)
        }

      }
      this.setData({
        post_station_id_list: post_station_id_list,
        post_table_arr: arr
      })
    }

  },
  // 查询部门
  getDepartmentData() {
    let url = this.data.baseUrl + "/get_department_list"

    let data = {
    }
    this.get(url, data).then(res => {
      if (res.code == 20000) {
        this.setData({
          options1: res.data

        })

      } else {
        this.setData({
          options1: [],
        })
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      this.setData({
        options1: [],
      })
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })

  },
  // 功能卡--字段数据双向绑定
  bindInput(e) {
    let lists = this.data.function_card_info_show
    let index = e.target.dataset.row
    let key = e.currentTarget.dataset.key
    lists[index][key] = e.detail
    this.setData({
      function_card_info_show: lists,
    })
  },
  // 新增功能卡按钮
  addFuncBtn() {
    let obj = {
      card: "",
      instruct: "",
      name: ""
    }
    let lists = this.data.function_card_info_show
    lists.push(obj)
    this.setData({
      function_card_info_show: lists
    })

  },
  // 删除按钮
  delFuncBtn(e) {
    console.log(e.target.dataset.row);
    let index = e.target.dataset.row
    let lists = this.data.function_card_info_show
    lists.splice(index, 1)
    this.setData({
      function_card_info_show: lists
    })

  },
  // 基础功能卡动作选择
  getPickerVlaue1(e) {
    console.log(e);
    let index = e.target.dataset.index
    let arr = JSON.parse(JSON.stringify(this.data.function_card_info_show))
    arr[index].instruct = e.detail.instruct_code
    arr[index].name = e.detail.instruct
    this.setData({
      function_card_info_show: arr,
      function_card_info: arr
    })
    this.deleteInstructName()
  },
  deleteInstructName() {
    let function_card_info = JSON.parse(JSON.stringify(this.data.function_card_info))
    for (let i = 0; i < function_card_info.length; i++) {
      delete function_card_info[i].name
    }
    this.setData({
      function_card_info: function_card_info
    })
  },
  // 查询途径货架信息
  getStorageData() {
    let url = this.data.baseUrl + "/get_storage_rack_station"
    let data = {
      department: this.data.department,
      current: 1,
      page_size: 999,
    }
    this.get(url, data).then(res => {
      if (res.code == 20000) {
        let name_list = []
        for (let i = 0; i < res.data.table_data.length; i++) {
          name_list.push(res.data.table_data[i].storage_rack_name)
        }
        this.setData({
          options_storage: res.data.table_data,
          options_storage_name: name_list,
        })
      } else {
        this.setData({
          options_storage: [],
          options_storage_name: [],
        })
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      this.setData({
        options_storage: [],
        options_storage_name: [],
      })
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })

  },
  // 查询岗位站点信息
  getPostData() {
    let url = this.data.baseUrl + "/get_post_station"

    let data = {
      department: this.data.department,
      current: 1,
      page_size: 999,
    }
    this.get(url, data).then(res => {
      console.log("查询岗位站点信息", res);
      if (res.code == 20000) {
        this.setData({
          post_options: res.data.table_data,
        })
      } else {
        this.setData({
          post_options: [],
        })
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      this.setData({
        post_options: [],
      })
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })

  },
  // 查询小车信息
  getAgvtData() {
    let url = this.data.baseUrl + "/get_magnetic_agv_info"

    let data = {
      department: this.data.department,
    }
    this.get(url, data).then(res => {
      if (res.code == 20000) {
        let agv_name_options = []
        for (let i = 0; i < res.data.table_data.length; i++) {
          agv_name_options.push(res.data.table_data[i].name)
        }
        this.setData({
          agv_options: res.data.table_data,
          agv_name_options: agv_name_options,
        })
      } else {
        this.setData({
          options2: [],
        })
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      this.setData({
        options2: [],
      })
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })

  },
  // 查询线体信息
  getLineData() {
    let url = this.data.baseUrl + "/get_line_list"

    let data = {
      department: this.data.department,
    }
    this.get(url, data).then(res => {
      if (res.code == 20000) {
        this.setData({
          options_line: res.data,
        })
      } else {
        this.setData({
          options_line: [],
        })
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      this.setData({
        options_line: [],
      })
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })

  },
  // 下拉选择
  getPickerVlaue(e) {
    let key = e.target.id
    if (key = 'department') {
      this.setData({
        [key]: e.detail
      })
      this.getPostData()
      this.getAgvtData()
      this.getStorageData()
      this.getLineData()
    }
    if (key = 'department') {
      this.setData({
        [key]: e.detail
      })

    }
  },

  // 提交按钮
  submit() {
    this.setData({
      submit_disabled: true
    })
    let url = this.data.baseUrl + "/add_magnetic_route"
    let data = {
      department: this.data.department,
      magnetic_route_name: this.data.magnetic_route_name,
      start_card: this.data.start_card,
      storage_rack_id_list: this.data.storage_rack_id_list,
      post_station_id_list: this.data.post_station_id_list,
      function_card_info: this.data.function_card_info,
      special_agv_list: this.data.special_agv_list,
    }
    console.log(data);
    if (!this.WxValidate.checkForm(data)) {
      const error = this.WxValidate.errorList[0]
      wx.showToast({
        title: error.msg,
        icon: 'none'
      })
      this.setData({
        submit_disabled: false
      })
    } else {
      this.post(url, data).then(res => {
        if (res.code == 20000) {
          wx.showToast({
            title: res.msg,
            icon: 'success'
          })
          this.setData({
            submit_disabled: false
          })
          this.reset()
        } else {
          this.setData({
            submit_disabled: false
          })
          wx.showToast({
            title: res.msg,
            icon: 'error'
          })
        }
      }).catch(err => {
        this.setData({
          submit_disabled: false
        })
        console.log(err);
        wx.showToast({
          title: err.msg,
          icon: 'error'
        })
      })

    }

  },
  //初始化表单验证规则
  initValidate() {
    let rules = {
      department: {
        required: true,
      },
      magnetic_route_name: {
        required: true,
      },

      start_card: {
        required: true,
      },

      // storage_rack_id_list: {
      //   required: true,
      // },
      // post_station_id_list: {
      //   required: true,
      // },
      function_card_info: {
        required: true,
      },
      special_agv_list: {
        required: true,
      },




    }

    let message = {
      department: {
        required: "所属部门-不能为空",
      },
      magnetic_route_name: {
        required: "路线名称-不能为空",
      },
      start_card: {
        required: "起始卡号-不能为空",
      },
      // storage_rack_id_list: {
      //   required: "线体-不能为空",
      // },
      // post_station_id_list: {
      //   required: "岗位名称-不能为空",
      // },
      function_card_info: {
        required: "基础功能卡-不能为空",
      },
      special_agv_list: {
        required: "分配车辆-不能为空",
      },






    }
    //实例化当前的验证规则和提示消息
    this.WxValidate = new WxValidate(rules, message);
  },
  // 重置按钮
  reset() {
    this.setData({
      baseUrl: "https://logisticsengineering.gree.com/agv_dispatch_system",
      function_card_info_show: [
        {
          card: "",
          instruct: ""
        }
      ],
      instruct_map: [
        { "instruct_code": 0, "instruct": "无动作" },
        { "instruct_code": 1, "instruct": "停车" },
        { "instruct_code": 2, "instruct": "左转" },
        { "instruct_code": 3, "instruct": "右转" },
        { 'instruct_code': 4, 'instruct': "高速" },
        { "instruct_code": 5, "instruct": "中速" },
        { "instruct_code": 6, "instruct": "低速" },
        { "instruct_code": 7, "instruct": "临停" },
      ],
      post_table_arr: [

      ],
      options1: [],
      activeNames1: [],
      activeNames2: [],
      activeNames3: [],
      activeNames4: [],
      selected_storage: [],
      department: "",
      magnetic_route_name: "",
      start_card: "",

      options_storage_name: [],
      agv_name_options: [],
      current_options_post: [],
      post_station_id_list: [],
      storage_rack_id_list: [],
      special_agv_list: [],
      agv_list: [],
      function_card_info: []
    })
    let childObj = this.selectComponent('.my-multiple')
    let childObj1 = this.selectComponent('.my-multiple1')
    childObj.reset()
    childObj1.reset()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.initValidate()
    this.getDepartmentData()
    // this.getPostData()
    // this.getAgvtData()
    // this.getStorageData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})